package com.zl.tby.kafka;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author changpeng
 * @version 1.0
 * @since 2024/9/29 8:54
 */
public class AnalysisUavData {

    public static UavNotify analysis(String content) throws Exception {
        UavNotify data = new UavNotify();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document document = factory.newDocumentBuilder().parse(new ByteArrayInputStream(content.getBytes()));
        Element rootElement = document.getDocumentElement();
        data.setCmdType(rootElement.getElementsByTagName("CmdType").item(0).getTextContent());
        data.setSn(rootElement.getElementsByTagName("SN").item(0).getTextContent());
        data.setDeviceID(rootElement.getElementsByTagName("DeviceID").item(0).getTextContent());
        data.setTime(rootElement.getElementsByTagName("Time").item(0).getTextContent());
        data.setMetaVersion(rootElement.getElementsByTagName("MetaVersion").item(0).getTextContent());
        data.setUav(getUavList((Element) rootElement.getElementsByTagName("UAV").item(0)));
        return data;
    }

    /**
     * 接续uavList，支持多个
     * @param uavElement: uav节点
     * @return
     */
    public static List<UavList> getUavList(Element uavElement) {
        List<UavList> uav = new ArrayList<>();
        NodeList uavNodeList = uavElement.getElementsByTagName("UAVList");
        for (int i = 0; i < uavNodeList.getLength(); i++) {
            Node node = uavNodeList.item(i);
            UavList uavList = new UavList(node.getAttributes().getNamedItem("Num").getTextContent());
            uavList.setItem(getUavListItem(((Element) node).getElementsByTagName("Item")));
            uav.add(uavList);
        }
        return uav;
    }

    /**
     * 获取uavList下的item，支持多个
     * @param nodeList
     * @return
     */
    public static List<UavListItem> getUavListItem(NodeList nodeList) {
        List<UavListItem> itemList = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            UavListItem item = new UavListItem();
            Element itemTag = (Element) nodeList.item(i);
            getPos(item, getElement(itemTag, "Pos"));
            getSpeed(item, getElement(itemTag, "Speed"));
            getAttitude(item, getElement(itemTag, "Attitude"));
            getHomePoint(item, getElement(itemTag, "HomePoint"));
            itemList.add(item);
        }
        return itemList;
    }

    private static void getPos(UavListItem item, Element pos) {
        if (pos != null) {
            item.setPosLongitude(getValue(pos, "Longitude"));
            item.setPosLatitude(getValue(pos, "Latitude"));
            item.setPosAltitude(getValue(pos, "Altitude"));
            item.setPosGpsStatus(getValue(pos, "GPSStatus"));
            item.setPosGpSAltType(getValue(pos, "GPSAltType"));
            item.setPosGpSCoorType(getValue(pos, "GPSCoorType"));
        }
    }

    private static void getSpeed(UavListItem item, Element speed) {
        if (speed != null) {
            item.setSpeedX(getValue(speed, "X"));
            item.setSpeedY(getValue(speed, "Y"));
            item.setSpeedZ(getValue(speed, "Z"));
        }
    }

    private static void getAttitude(UavListItem item, Element attitude) {
        if (attitude != null) {
            item.setAttitudePitch(getValue(attitude, "Pitch"));
            item.setAttitudeRoll(getValue(attitude, "Roll"));
            item.setAttitudeYaw(getValue(attitude, "Yaw"));
            item.setAttitudeHeading(getValue(attitude, "Heading"));
        }
    }

    private static void getHomePoint(UavListItem item, Element homePoint) {
        if (homePoint != null) {
            item.setHomePointLongitude(getValue(homePoint, "Longitude"));
            item.setHomePointLatitude(getValue(homePoint, "Latitude"));
            item.setHomePointAltitude(getValue(homePoint, "Altitude"));
            item.setHomePointGpsStatus(getValue(homePoint, "GPSStatus"));
            item.setHomePointGpSAltType(getValue(homePoint, "GPSAltType"));
            item.setHomePointGpSCoorType(getValue(homePoint, "GPSCoorType"));
        }
    }

    public static Element getElement(Element itemTag, String key) {
        NodeList nodeList = itemTag.getElementsByTagName(key);
        if (nodeList != null && nodeList.getLength() > 0) {
            return (Element) nodeList.item(0);
        }
        return null;
    }

    public static String getValue(Element ele, String key) {
        NodeList nodeList = ele.getElementsByTagName(key);
        if (nodeList != null && nodeList.getLength() > 0) {
            return nodeList.item(0).getTextContent();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        UavNotify data = AnalysisUavData.analysis(content);
        System.out.println("cmdType： " + data.getCmdType());
        System.out.println("sn： " + data.getSn());
        System.out.println("deviceId： " + data.getDeviceID());
        System.out.println("time： " + data.getTime());
        System.out.println("metaVersion： " + data.getMetaVersion());
    }

    public static String content = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" +
            "<Notify>\n" +
            "\t<CmdType>DataTransfer</CmdType>\n" +
            "\t<SN>431</SN>\n" +
            "\t<DeviceID>23122600102000090004</DeviceID>\n" +
            "\t<Time>2024-05-28T16:46:22</Time>\n" +
            "\t<MetaVersion>v2</MetaVersion>\n" +
            "\t<UAV>\n" +
            "\t\t<UAVList Num=\"1\">\n" +
            "\t\t\t<Item>\n" +
            "\t\t\t\t<Pos>\n" +
            "\t\t\t\t\t<Longitude>Pos_117.3607244984</Longitude>\n" +
            "\t\t\t\t\t<Latitude>Pos_25.9455340508</Latitude>\n" +
            "\t\t\t\t\t<Altitude>Pos_193871</Altitude>\n" +
            "\t\t\t\t\t<GPSStatus>Pos_1</GPSStatus>\n" +
            "\t\t\t\t\t<GPSAltType>Pos_0</GPSAltType>\n" +
            "\t\t\t\t\t<GPSCoorType>Pos_0</GPSCoorType>\n" +
            "\t\t\t\t</Pos>\n" +
            "\t\t\t\t<Speed>\n" +
            "\t\t\t\t\t<X>0</X>\n" +
            "\t\t\t\t\t<Y>0</Y>\n" +
            "\t\t\t\t\t<Z>0</Z>\n" +
            "\t\t\t\t</Speed>\n" +
            "\t\t\t\t<Attitude>\n" +
            "\t\t\t\t\t<Pitch>-29</Pitch>\n" +
            "\t\t\t\t\t<Roll>1</Roll>\n" +
            "\t\t\t\t\t<Yaw>1719</Yaw>\n" +
            "\t\t\t\t\t<Heading>171</Heading>\n" +
            "\t\t\t\t</Attitude>\n" +
            "\t\t\t\t<HomePoint>\n" +
            "\t\t\t\t\t<Longitude>117.3607351284</Longitude>\n" +
            "\t\t\t\t\t<Latitude>25.9456848409</Latitude>\n" +
            "\t\t\t\t\t<Altitude>193871</Altitude>\n" +
            "\t\t\t\t\t<GPSStatus>0</GPSStatus>\n" +
            "\t\t\t\t\t<GPSAltType>1</GPSAltType>\n" +
            "\t\t\t\t\t<GPSCoorType>0</GPSCoorType>\n" +
            "\t\t\t\t</HomePoint>\n" +
            "\t\t\t</Item>\n" +
            "\t\t\t<Item>\n" +
            "\t\t\t\t<Pos>\n" +
            "\t\t\t\t\t<Longitude>Pos1_117.3607244984</Longitude>\n" +
            "\t\t\t\t\t<Latitude>Pos1_25.9455340508</Latitude>\n" +
            "\t\t\t\t\t<Altitude>Pos1_193871</Altitude>\n" +
            "\t\t\t\t\t<GPSStatus>Pos1_1</GPSStatus>\n" +
            "\t\t\t\t\t<GPSAltType>Pos1_0</GPSAltType>\n" +
            "\t\t\t\t\t<GPSCoorType>Pos1_0</GPSCoorType>\n" +
            "\t\t\t\t</Pos>\n" +
            "\t\t\t\t<Speed>\n" +
            "\t\t\t\t\t<X>10</X>\n" +
            "\t\t\t\t\t<Y>10</Y>\n" +
            "\t\t\t\t\t<Z>10</Z>\n" +
            "\t\t\t\t</Speed>\n" +
            "\t\t\t\t<Attitude>\n" +
            "\t\t\t\t\t<Pitch>-30</Pitch>\n" +
            "\t\t\t\t\t<Roll>11</Roll>\n" +
            "\t\t\t\t\t<Yaw>17191</Yaw>\n" +
            "\t\t\t\t\t<Heading>1711</Heading>\n" +
            "\t\t\t\t</Attitude>\n" +
            "\t\t\t\t<HomePoint>\n" +
            "\t\t\t\t\t<Longitude>117.3607351284</Longitude>\n" +
            "\t\t\t\t\t<Latitude>25.9456848409</Latitude>\n" +
            "\t\t\t\t\t<Altitude>193871</Altitude>\n" +
            "\t\t\t\t\t<GPSStatus>0</GPSStatus>\n" +
            "\t\t\t\t\t<GPSAltType>1</GPSAltType>\n" +
            "\t\t\t\t\t<GPSCoorType>0</GPSCoorType>\n" +
            "\t\t\t\t</HomePoint>\n" +
            "\t\t\t</Item>\n" +
            "\t\t</UAVList>\n" +
            "\t</UAV>\n" +
            "</Notify>";

}
